package com.shensi.adminCollect.controller.label;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.label.*;
import com.shensi.adminCollect.service.label.ICustomerLabelDemoService;
import com.shensi.adminCollect.service.label.ICustomerLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

/**
 * 用户标签Controller
 *
 * @author qujia
 * @date 2021-06-15
 */
@RestController
@RequestMapping("/label/label")
@Api(value = "LabelController", description = "客户标签Controller")
public class CustomerLabelController extends BaseController
{
    @Autowired
    private ICustomerLabelService customerLabelService;
    @Autowired
    private ICustomerLabelDemoService customerLabelDemoService;


    /**
     * 查询用户标签列表
     */
    @GetMapping("/list")
    @ApiOperation("查询标签列表")
    public TableDataInfo list(CustomerLabel customerLabel)
    {

        startPage();
        List<CustomerLabel> list = customerLabelService.selectCustomerLabelList(customerLabel);
        return getDataTable(list);
    }

    /**
     * 导出用户标签列表
     */
    @GetMapping("/export")
    public AjaxResult export(CustomerLabel customerLabel)
    {
        List<CustomerLabel> list = customerLabelService.selectCustomerLabelList(customerLabel);
        ExcelUtil<CustomerLabel> util = new ExcelUtil<CustomerLabel>(CustomerLabel.class);
        return util.exportExcel(list, "label");
    }

    /**
     * 获取用户标签详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("id获取用户标签详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(customerLabelService.selectCustomerLabelById(id));
    }

    /**
     * 获取用户标签详细信息
     */
    @GetMapping(value = "/selectCustomerLabelInfo")
    @ApiOperation("其他方式获取用户标签详细信息")
    public AjaxResult selectCustomerLabelInfo(CustomerLabel customerLabel)
    {
        System.out.println("==================="+new Date());
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        customerLabel.setDelFlag("0");
        ResponeCustomerLabel responeCustomerLabel =new ResponeCustomerLabel();
        List<CustomerLabel> notlist = customerLabelService.selectNotCustomerLabelInfo(customerLabel);
        List<CustomerLabel> list = customerLabelService.selectPhoneList(customerLabel);
        responeCustomerLabel.setNotCustomerLabelList(notlist);
       // responeCustomerLabel.setPhoneList(list);
        System.out.println("=================================="+new Date());
        return AjaxResult.success(responeCustomerLabel);
    }

    /**
     * 查询没有绑定标签的客户
     * @param customerLabel
     * @return
     */
    @GetMapping(value = "/selectNotCustomerLabelInfo")
    @ApiOperation("查询没有绑定标签的客户")
    public TableDataInfo selectNotCustomerLabelInfo(CustomerLabel customerLabel)
    {
        startPage();
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        customerLabel.setDelFlag("0");
        List<CustomerLabel> notlist = customerLabelService.selectNotCustomerLabelInfo(customerLabel);
        return getDataTable(notlist);
    }

    /**
     * 查询以绑定标签的客户信息
     * @param customerLabel
     * @return
     */
    @GetMapping(value = "/selectPhoneList")
    @ApiOperation("查询绑定标签的客户")
    public TableDataInfo selectPhoneList(CustomerLabel customerLabel)
    {
        startPage();
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setDelFlag("0");
        customerLabel.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        List<CustomerLabel> list = customerLabelService.selectPhoneList(customerLabel);
        return getDataTable(list);
    }

    /**
     * 查询绑定标签的客户导出
     * @param customerLabel
     * @return
     */
    @GetMapping(value = "/selectPhoneListExport")
    @ApiOperation("查询绑定标签的客户导出")
    public AjaxResult selectPhoneListExport(CustomerLabel customerLabel)
    {
        startPage();
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setDelFlag("0");
        customerLabel.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        List<CustomerLabel> list = customerLabelService.selectPhoneList(customerLabel);
        ExcelUtil<CustomerLabel> util = new ExcelUtil<>(CustomerLabel.class);
        return util.exportExcel(list, "label");
    }


    /**
     * 查询标签是否绑定了人
     * @param customerLabel
     * @return
     */
    @GetMapping(value = "/selectLabel")
    @ApiOperation("查询标签是否绑定了人")
    public AjaxResult selectLabel(CustomerLabel customerLabel)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        customerLabel.setDelFlag("0");
        List<CustomerLabel> list = customerLabelService.selectCustomerLabelList(customerLabel);
        boolean flag =true;
        if(list!=null && list.size()>0){
            flag=false;
        }
        return AjaxResult.success(flag);
    }

    /**
     * 新增用户标签
     */
    @PostMapping
    @ApiOperation("新增用户标签")
    public AjaxResult add(@RequestBody CustomerLabel customerLabel)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setCreateBy(currentUser.getUser().getUserId()+"");
        customerLabel.setDelFlag("0");
        return toAjax(customerLabelService.insertCustomerLabel(customerLabel));
    }

    /**
     * 修改用户标签
     */
    @PutMapping
    @ApiOperation("修改用户标签")
    public AjaxResult edit(@RequestBody CustomerLabel customerLabel)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerLabel.setUpdateBy(currentUser.getUser().getUserId()+"");
        return toAjax(customerLabelService.updateCustomerLabel(customerLabel));
    }

    /**
     * 打用户标签
     */
    @PostMapping("/addAndDelCustomerLabel")
    @ApiOperation("打标签")
    @Transactional
    public AjaxResult addAndDelCustomerLabel(@RequestBody RequstCustomerLabel requstCustomerLabel)
    {
        int i =1;
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if(requstCustomerLabel!=null && requstCustomerLabel.getFlag()!=null && requstCustomerLabel.getFlag().equals("0")){
            if(requstCustomerLabel.getPhoneList()!=null && requstCustomerLabel.getPhoneList().size()>0){
                for(String phone:requstCustomerLabel.getPhoneList()){
                    CustomerLabel customerLabel =new CustomerLabel();

                    customerLabel.setEnterpriseId(currentUser.getUser().getEnterpriseId());
                    customerLabel.setDelFlag("0");
                    customerLabel.setPhone(phone);
                    customerLabel.setLabelId(requstCustomerLabel.getLabelId());
                    customerLabel.setCreateBy(currentUser.getUser().getUserId()+"");
                  i= customerLabelService.insertCustomerLabel(customerLabel);
                }
            }
        }else if(requstCustomerLabel!=null && requstCustomerLabel.getFlag()!=null && requstCustomerLabel.getFlag().equals("1")){
            if(requstCustomerLabel.getPhoneList()!=null && requstCustomerLabel.getPhoneList().size()>0){
                for(String phone:requstCustomerLabel.getPhoneList()){
                    CustomerLabel customerLabel =new CustomerLabel();
                    customerLabel.setEnterpriseId(currentUser.getUser().getEnterpriseId());
                    customerLabel.setDelFlag("1");
                    customerLabel.setPhone(phone);
                    customerLabel.setLabelId(requstCustomerLabel.getLabelId());
                    customerLabel.setUpdateBy(currentUser.getUser().getUserId()+"");
                    i=  customerLabelService.editCustomerLabel(customerLabel);
                }
            }
        }
        return toAjax(i);
    }



    /**
     * 删除用户标签
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除用户标签")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(customerLabelService.deleteCustomerLabelByIds(ids));
    }

    /**
     * 查询标签用户
     */
    @GetMapping(value = "/selectCustomerLabelInfoList")
    @ApiOperation("查询标签用户")
    public TableDataInfo selectCustomerLabelInfoList(CustomerLabelDemo customerLabelDemo)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        //customerLabelDemo.setStationId(currentUser.getUser().getDeptId());
        //customerLabelDemo.setStationIdList(sysStationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId()));
        customerLabelDemo.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        startPage();
        List<CustomerLabelDemo> list = new ArrayList<>();

         if(customerLabelDemo.getSecondFlag()!=null && customerLabelDemo.getSecondFlag().equals("1")) {
             if(customerLabelDemo.getEndCountNum()!=null&& customerLabelDemo.getEndCountNum()==0){
                 list=customerLabelDemoService.selectNewAppUser(customerLabelDemo);
             }else {
                 //开启二次查询
                 list = customerLabelDemoService.secondCustomerLabelDemoList(customerLabelDemo);
             }
        }else if(customerLabelDemo.getSecondFlag()!=null && customerLabelDemo.getSecondFlag().equals("0")) {
              if(customerLabelDemo.getEndCountNum()!=null&& customerLabelDemo.getEndCountNum()==0){
                  list=customerLabelDemoService.selectNewAppUser(customerLabelDemo);
              }else {
                  list = customerLabelDemoService.selectCustomerLabelDemoList(customerLabelDemo);
              }
         }

        return getDataTable(list);
    }


    /**
     * 导出用户标签列表
     */
    @GetMapping("/selectCustomerLabelInfoListExport")
    @ApiOperation("导出用户标签列表")
    public AjaxResult selectCustomerLabelInfoListExport(CustomerLabelDemo customerLabelDemo)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        //customerLabelDemo.setStationId(currentUser.getUser().getDeptId());
        //customerLabelDemo.setStationIdList(sysStationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId()));
        customerLabelDemo.setEnterpriseId(currentUser.getUser().getEnterpriseId());
//        customerLabelDemo.setPageSizeLimit(1000000);
        List<CustomerLabelDemo> list = new ArrayList<>();

        if(customerLabelDemo.getSecondFlag()!=null && customerLabelDemo.getSecondFlag().equals("1")) {
            if(customerLabelDemo.getEndCountNum()!=null&& customerLabelDemo.getEndCountNum()==0){
                list=customerLabelDemoService.selectNewAppUser(customerLabelDemo);
            }else {
                //开启二次查询
                list = customerLabelDemoService.secondCustomerLabelDemoList(customerLabelDemo);
            }
        }else if(customerLabelDemo.getSecondFlag()!=null && customerLabelDemo.getSecondFlag().equals("0")) {
            if(customerLabelDemo.getEndCountNum()!=null&& customerLabelDemo.getEndCountNum()==0){
                list=customerLabelDemoService.selectNewAppUser(customerLabelDemo);
            }else {
                list = customerLabelDemoService.selectCustomerLabelDemoList(customerLabelDemo);
            }
        }
        ExcelUtil<CustomerLabelDemo> util = new ExcelUtil<CustomerLabelDemo>(CustomerLabelDemo.class);
        return util.exportExcel(list, "label");
    }


    /**
     * 修改用户标签
     */
    @PostMapping("/delCustomerLabel")
    @Transactional
    @ApiOperation("修改用户标签")
    public AjaxResult delCustomerLabel(@RequestBody CustomerLabelDemo customerLabelDemo)
    {
        int i =1;
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if(customerLabelDemo.getPhoneList()!=null && customerLabelDemo.getPhoneList().size()>0){
            //根据手机号查询客户标签
            customerLabelDemo.setDelFlag("0");
            customerLabelDemo.setEnterpriseId(currentUser.getUser().getEnterpriseId());
            List<CustomerLabelDemo> demoList = customerLabelDemoService.selectPhoneCustomerLabelList(customerLabelDemo);
            if(demoList!=null &&demoList.size()>0){
                for(CustomerLabelDemo demo:demoList){
                    CustomerLabel customerLabel =new CustomerLabel();
                    customerLabel.setDelFlag("1");
                    customerLabel.setUpdateBy(currentUser.getUser().getUserId()+"");
                    customerLabel.setId(demo.getId());
                    i=  customerLabelService.updateCustomerLabel(customerLabel);
                }
            }
        }
        return toAjax(i);
    }
}
